package com.wyzz.hr.util;

import java.util.*;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;

import com.wyzz.hr.bo.IEmpBenefitPlanBo;
import com.wyzz.hr.bo.ILeavecalendarBO;
import com.wyzz.hr.bo.ISalaryPaidBo;
import com.wyzz.hr.bo.impl.ExaminDateUtil;
import com.wyzz.hr.pojo.Attendmonthly;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Empsalaryconfig;
import com.wyzz.hr.pojo.Empsalarypay;
import com.wyzz.hr.pojo.Leavebalance;
import com.wyzz.hr.pojo.Leavetype;
import com.wyzz.hr.spring.SpringBeanFactory;

public class InterpreterExecuteContext {

	public InterpreterExecuteContext(String yearMonth) {
		Map dbMap = DatabaseSysConfigManager.getInstance().getProperties();
		sysConfigVarDef = new SysConfigVarDef(dbMap);
		if (StringUtils.isEmpty(yearMonth))
			yearMonth = DateUtil.convDateToYM(new Date());
		monthDateArr = ExaminDateUtil.getDateArray(yearMonth, sysConfigVarDef
				.getMonthSum());

		joinYearCalc = sysConfigVarDef.getJoinYearCalc();
		ILeavecalendarBO lc_BO = (ILeavecalendarBO) SpringBeanFactory
				.getBean("leavecalendarBO");
		lcList = lc_BO.getCalendarListByDay(monthDateArr[0], monthDateArr[1]);
	}

	public Leavebalance execute(Leavetype leavetype, Employee employee)
			throws InterpreterException {
		Interpreter interpreter = new Interpreter();
		interpreter.setSysConfigVarDef(sysConfigVarDef);
		DateVarDef dateVarDef = new DateVarDef(employee, monthDateArr,
				joinYearCalc, lcList);

		interpreter.setDateVarDef(dateVarDef);
		Leavebalance leavebalance = new Leavebalance();
		if (leavetype.getLtSpecialCat().intValue() == 1) {
			if (StringUtils.isNotEmpty(leavetype.getLtDaysOfYear()))
				leavebalance.setLbDaysOfYear(interpreter
						.calculateLeave(leavetype.getLtDaysOfYear()));
			if (StringUtils.isNotEmpty(leavetype.getLtHoursOfYear()))
				leavebalance.setLbHoursOfYear(interpreter
						.calculateLeave(leavetype.getLtHoursOfYear()));
			if (StringUtils.isNotEmpty(leavetype.getLtDaysForRelease()))
				leavebalance.setLbDaysForRelease(interpreter
						.calculateLeave(leavetype.getLtDaysForRelease()));
			if (StringUtils.isNotEmpty(leavetype.getLtHoursForRelease()))
				leavebalance.setLbHoursForRelease(interpreter
						.calculateLeave(leavetype.getLtHoursForRelease()));
			if (StringUtils.isNotEmpty(leavetype.getLtOtherParameter()))
				leavebalance.setLbOtherDays(interpreter
						.calculateLeave(leavetype.getLtOtherParameter()));
			if (leavetype.getLtBalForward() != null)
				leavebalance.setLbBalEndDate(leavetype.getLtBalForward());
		} else if (leavetype.getLtSpecialCat().intValue() == 5) {
			if (StringUtils.isNotEmpty(leavetype.getLtDaysOfYear()))
				leavebalance.setLbDaysOfYear(interpreter
						.calculateLeave(leavetype.getLtDaysOfYear()));
			if (StringUtils.isNotEmpty(leavetype.getLtHoursOfYear()))
				leavebalance.setLbHoursOfYear(interpreter
						.calculateLeave(leavetype.getLtHoursOfYear()));
			if (StringUtils.isNotEmpty(leavetype.getLtDaysForRelease()))
				leavebalance.setLbDaysForRelease(interpreter
						.calculateLeave(leavetype.getLtDaysForRelease()));
			if (StringUtils.isNotEmpty(leavetype.getLtHoursForRelease()))
				leavebalance.setLbHoursForRelease(interpreter
						.calculateLeave(leavetype.getLtHoursForRelease()));
		} else if (leavetype.getLtSpecialCat().intValue() == 11
				&& StringUtils.isNotEmpty(leavetype.getLtOtherParameter()))
			leavebalance.setLbOtherDays(interpreter.calculateLeave(leavetype
					.getLtOtherParameter()));
		return leavebalance;
	}

	public void execute(Empsalaryconfig configArr[]) {
		for (Empsalaryconfig config : configArr) {
			List acctItems = config.getAcctItems();
			Interpreter interpreter = new Interpreter();
			interpreter.setSalaryConfig(config);
			interpreter.setFillDataMode(0);
			interpreter.setSysConfigVarDef(this.sysConfigVarDef);
			DateVarDef dateVarDef = new DateVarDef(config.getEmployee(),
					this.monthDateArr, this.joinYearCalc, this.lcList);
			interpreter.setDateVarDef(dateVarDef);
			BenefitVarDef bfVarDef = new BenefitVarDef(config.getEmployee()
					.getBenefit(), config.getEmployee().getEmpBenefitType(),
					config.getEscJobgrade());
			interpreter.setBenefitVarDef(bfVarDef);
			try {
				interpreter.calculateSalary(acctItems, true);
			} catch (InterpreterException ie) {
				List<String> ieList = ie.getErrorList();
				for (String msg : ieList) {
					System.out.println(config.getEmployee().getEmpName()
							+ " 薪资配置错误-SalaryConfigError" + msg);
				}
				return;
			}
			for (int a = acctItems.size(); a < 48; ++a)
				try {
					PropertyUtils
							.setProperty(config, (new StringBuilder()).append(
									"escColumn").append(a + 1).toString(), null);
				} catch (Exception e) {
					e.printStackTrace();
				}
		}
	}

	public void execute(String currentEmp, boolean add0And1,
			Empsalarypay payArray[]) {
		Empsalarypay arr$[] = payArray;
		int len$ = arr$.length;
		for (int i$ = 0; i$ < len$; i$++) {
			Empsalarypay pay = arr$[i$];
			List acctItems = pay.getAcctItems();
			Attendmonthly attendMonthly = pay.getAttendMonthly();
			Interpreter interpreter = new Interpreter();
			interpreter.setSalaryPay(pay);
			interpreter.setFillDataMode(1);
			interpreter.setSysConfigVarDef(sysConfigVarDef);
			DateVarDef dateVarDef = new DateVarDef(pay.getEspEmpno(),
					monthDateArr, joinYearCalc, lcList);
			interpreter.setDateVarDef(dateVarDef);
			BenefitVarDef bfVarDef = new BenefitVarDef(pay.getBenefit(), pay
					.getBenefitType(), pay.getEspJobgrade());
			interpreter.setBenefitVarDef(bfVarDef);
			ExaminVarDef examinVardef = new ExaminVarDef(attendMonthly);
			interpreter.setExaminVarDef(examinVardef);
			PerfVarDef perfVardef = new PerfVarDef(pay.getEspEmpno(), pay
					.getPerfplans());
			interpreter.setPerfVarDef(perfVardef);
			try {
				interpreter.calculateSalary(acctItems, true);
				if (pay.getEspBenefitPlans().intValue() > 0) {
					ISalaryPaidBo salaryPaidBo = (ISalaryPaidBo) SpringBeanFactory
							.getBean("salaryPaidBo");
					salaryPaidBo.addAddtionalEbpToPay(pay, add0And1);
					interpreter.calculateSalary(acctItems, false);
					pay.setEspChanged(Integer.valueOf(1));
				}
				IEmpBenefitPlanBo ebpBo = (IEmpBenefitPlanBo) SpringBeanFactory
						.getBean("empbenefitplanBo");
				ebpBo.addCurrPlan(currentEmp, pay);
			} catch (InterpreterException ie) {
				List ieList = ie.getErrorList();
				String msg;
				for (Iterator it = ieList.iterator(); it.hasNext(); System.out
						.println((new StringBuilder())
								.append(pay.getEspEmpno().getEmpName())
								.append(
										" \u85AA\u8D44\u914D\u7F6E\u9519\u8BEF-SalaryPayError")
								.append(msg).toString()))
					msg = (String) it.next();

				return;
			}
			for (int i = acctItems.size(); i < 48; i++)
				try {
					PropertyUtils
							.setProperty(pay, (new StringBuilder()).append(
									"espColumn").append(i + 1).toString(), null);
				} catch (Exception e) {
					e.printStackTrace();
				}

		}

	}

	private SysConfigVarDef sysConfigVarDef;
	private Date monthDateArr[];
	private Integer joinYearCalc;
	private List lcList;
}
